package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.service.SmsService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;

@RestController
@RequestMapping("/sms")
public class SmsController {
    
    @Reference
    private SmsService smsService;
    
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    /*
    * 
    * 登录短信验证码
    * */
    @PostMapping("/sendLoginCode")
    public Result sendLoginCode(@RequestParam("phone") String phoneNumber) {
        /*try {

            Boolean result=smsService.sendCheckCode(phoneNumber);

            if (true==result) {

                return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);*/

        String redisKey = "REDIS_KEY_" + phoneNumber;

        //先判断该手机号发送的验证码是否还未失效
        if(this.redisTemplate.hasKey(redisKey)){
            String msg = "上一次发送的验证码还未失效！";
            return new Result(false,msg);
        }

        //短信发送的验证码
        //String code = this.sendSms(phone);
        
        //生成6位随机数字验证码
        String code = RandomStringUtils.randomNumeric(6);
        System.out.println("登录短信验证码为："+code);

        if(StringUtils.isEmpty(code)){
          
            return new Result(false,MessageConstant.SEND_VALIDATECODE_FAIL);
        }

        //短信发送成功，将验证码保存到redis中，有效期为5分钟
        this.redisTemplate.opsForValue().set(redisKey, code, Duration.ofMinutes(5));

        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        
    }
    
    /*
    * 
    * 体检预约短信验证码
    * */
    @PostMapping("/sendCode")
    public Result sendCode(@RequestParam("phone") String telephone) {
 
        String redisKey = "REDIS_KEY_YUYUE" + telephone;

        //先判断该手机号发送的验证码是否还未失效
        if(this.redisTemplate.hasKey(redisKey)){
            String msg = "上一次发送的验证码还未失效！";
            return new Result(false,msg);
        }

        //短信发送的验证码
        //String code = this.sendSms(phone);

        String code = RandomStringUtils.randomNumeric(6);
        System.out.println("预约短信验证码为："+code);
        if(StringUtils.isEmpty(code)){
            //String msg = "发送短信验证码失败！";
            return new Result(false,MessageConstant.SEND_VALIDATECODE_FAIL);
        }

        //短信发送成功，将验证码保存到redis中，有效期为5分钟
        this.redisTemplate.opsForValue().set(redisKey, code, Duration.ofMinutes(5));

        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    } 
    
}
